home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / screen-profiles / ec2-cost < prev    next >
Encoding:
Text File  |  2009-04-08  |  3.4 KB  |  99 lines

  1. #!/bin/sh
  2. #
  3. #    ec2-cost: approximate EC2 cost (USD) of the current instance
  4. #    Copyright (C) 2008 Canonical Ltd.
  5. #
  6. #    Authors: Dustin Kirkland <kirkland@canonical.com>
  7. #
  8. #    This program is free software: you can redistribute it and/or modify
  9. #    it under the terms of the GNU General Public License as published by
  10. #    the Free Software Foundation, version 3 of the License.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20. FORCE=0
  21. DETAIL=0
  22.  
  23. # Default is "off"
  24. p="ec2-cost"
  25. grep -qs "^$p=1$" "$HOME/.screen-profiles/status" && FORCE=1
  26. grep -qs "^$p=0$" "$HOME/.screen-profiles/status" && FORCE=0
  27.  
  28. for arg in $@; do
  29.     case "$arg" in
  30.         -f|--force)
  31.             FORCE=1
  32.         ;;
  33.         -d|--detail)
  34.             DETAIL=1
  35.         ;;
  36.     esac
  37. done
  38.  
  39. # Exit immediately if this is not an Amazon EC2 instance, we're not
  40. # manually turned on, and we're not in force mode
  41. [ -r "/etc/ec2-version" -o -r "$HOME/.screen-profiles/ec2-cost" -o "$FORCE" = "1" ]
  42. [ "$?" = "0" ] || exit 0
  43.  
  44. # Approximate Instance Cost Basis
  45. #            US        Europe
  46. # Small  (1cpu, 32bit)    $0.10/h        $0.11/h
  47. # Medium (2cpu, 32bit)    $0.20/h        $0.22/h
  48. # Large  (4cpu, 64bit)    $0.40/h        $0.44/h
  49. # XLarge (8cpu, 64bit)    $0.80/h        $0.88/h
  50.  
  51. # Count CPUs
  52. cpu_count=`grep -c "^processor.*:" /proc/cpuinfo`
  53. [ -z "$cpu_count" ] && cpu_count=1
  54. # Apply the going rate
  55. CPU_RATE=`echo "$cpu_count" | awk '{printf "%f", 0.10*$1}'`
  56. # BUG: Some logic needed here to add 10% cost for Europe instances?
  57.  
  58. # Data Transfer Cost Basis
  59. # Incoming    $0.10/GB
  60. # Outgoing    $0.17/GB
  61. # (This gets more complex if you use >1TB/mo)
  62. RX_RATE="0.10"
  63. TX_RATE="0.17"
  64.  
  65. # Auto detect network interface
  66. IF=`route -n | grep "0\.0\.0\.0" | tail -n1 | awk '{print $8}'`
  67.  
  68. # Calculate bandwidth cost
  69. tx_gb=`/sbin/ifconfig "$IF" | grep "TX bytes:" | sed "s/^.*TX bytes://" | awk '{ printf "%f", $1 / 1024 / 1024 / 1024 }'`
  70. rx_gb=`/sbin/ifconfig "$IF" | grep "RX bytes:" | sed "s/^.*RX bytes://" | awk '{ printf "%f", $1 / 1024 / 1024 / 1024 }'`
  71. network_cost=`echo "$tx_gb" "$TX_RATE" "$rx_gb" "$RX_RATE" | awk '{printf "%f %f", $1*$2, $3*$4}' | awk '{printf "%f", $1 + $2}'`
  72.  
  73. # Calculate uptime cost
  74. # BUG: This will only calculate uptime since boot!
  75. #      Some additional input will be required to account for reboots!!!
  76. hours=`awk '{printf "%f", 1 + $1 / 60 / 60 }' /proc/uptime | sed 's/\..*$//' `
  77. uptime_cost=`echo "$hours" | awk "{printf \"%f\", "$CPU_RATE" * $hours}"`
  78. total_cost=`echo "$network_cost" "$uptime_cost" | awk '{printf "~\$%.2f", $1 + $2}'`
  79.  
  80. if [ "$DETAIL" = "1" ]; then
  81.     echo
  82.     echo "================================================"
  83.     echo "Estimated cost in Amazon's EC2 since last reboot"
  84.     echo "================================================"
  85.     echo "  Network sent:  $tx_gb GB   @ \$$RX_RATE/GB"
  86.     echo "  Network recv:  $rx_gb GB   @ \$$TX_RATE/GB"
  87.     echo "  Network cost:  \$$network_cost"
  88.     echo "------------------------------------------------"
  89.     echo "  Uptime:        $hours hr  @ \$$CPU_RATE/hr"
  90.     echo "  Uptime cost:   \$$uptime_cost"
  91.     echo "------------------------------------------------"
  92.     echo "Total cost:      $total_cost"
  93.     echo "================================================"
  94.     echo
  95.     exit 0
  96. fi
  97.  
  98. printf "\005{= wg}%s\005{-} " $total_cost
  99.